package com.example.demo.config;

import com.example.demo.Dto.UpdateOrderDto;
import com.example.demo.Service.OrderService;
import com.example.demo.Util.ThreadPoolUtil;
import com.example.demo.VO.OrderInfoVo;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {
    @Autowired
    private OrderService orderService;
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delay.queue", durable = "true"),
            exchange = @Exchange(name = "order-exchange", delayed = "true"),
            key = "delay"
    ))
    public void listenDelayQueue(String msg){
        ThreadPoolUtil.execute(() -> {
            OrderInfoVo orderById = orderService.getOrderById(msg);
            Integer status = orderById.getStatus();
            if (status == 0 || status == 1){
                UpdateOrderDto updateOrderDto = new UpdateOrderDto();
                updateOrderDto.setOrderId(msg);
                updateOrderDto.setStatus(4);
                orderService.updateOrder(updateOrderDto);
            }
        });
    }
}
